/*
' Copyright (c) 2014 Diadrasis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using net.diadrasis.dnn.jsImageGallery.Components;


namespace net.diadrasis.dnn.jsImageGallery.Data
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// SQL Server implementation of the abstract DataProvider class
    /// 
    /// This concreted data provider class provides the implementation of the abstract methods 
    /// from data dataprovider.cs
    /// 
    /// In most cases you will only modify the Public methods region below.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public class SqlDataProvider : DataProvider
    {

        #region Private Members

        private const string ProviderType = "data";
        private const string ModuleQualifier = "jsImageGallery_";

        private readonly ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private readonly string _connectionString;
        private readonly string _providerPath;
        private readonly string _objectQualifier;
        private readonly string _databaseOwner;

        #endregion

        #region Constructors

        public SqlDataProvider()
        {

            // Read the configuration specific information for this provider
            Provider objProvider = (Provider)(_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);

            // Read the attributes for this provider

            //Get Connection string from web.config
            _connectionString = Config.GetConnectionString();

            if (string.IsNullOrEmpty(_connectionString))
            {
                // Use connection string specified in provider
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];

            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (!string.IsNullOrEmpty(_objectQualifier) && _objectQualifier.EndsWith("_", StringComparison.Ordinal) == false)
            {
                _objectQualifier += "_";
            }

            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (!string.IsNullOrEmpty(_databaseOwner) && _databaseOwner.EndsWith(".", StringComparison.Ordinal) == false)
            {
                _databaseOwner += ".";
            }

        }

        #endregion

        #region Properties

        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
        }

        public string ProviderPath
        {
            get
            {
                return _providerPath;
            }
        }

        public string ObjectQualifier
        {
            get
            {
                return _objectQualifier;
            }
        }

        public string DatabaseOwner
        {
            get
            {
                return _databaseOwner;
            }
        }

        private string NamePrefix
        {
            get { return DatabaseOwner + ObjectQualifier + ModuleQualifier; }
        }

        #endregion

        #region Private Methods

        private static object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        #endregion

        #region Public Methods

        public override System.Data.IDataReader GetImages(int moduleId)
        {                 
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "getImages",
                new SqlParameter("@moduleID", moduleId));
        }

        public override System.Data.IDataReader GetSectionImages(int imgsecId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "getSectionImages",
                new SqlParameter("@imgsecID", imgsecId));
        }

        public override System.Data.IDataReader GetImage(int imgId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "getImage",
                new SqlParameter("@imgID", imgId));
        }

        public override int AddImage(cImage img)
        {
            return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, NamePrefix + "addImage",
                 new SqlParameter("@imgFile", img.file),
                 new SqlParameter("@imgDesc", img.desc),
                 new SqlParameter("@imgLink", img.link),
                 new SqlParameter("@moduleID", img.modID),
                 new SqlParameter("@imgsecID", img.imgsecID)));

        }

        public override void DeleteImage(int imgId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "deleteImage",
                new SqlParameter("@imgID", imgId));
        }

        public override void UpdateImage(cImage img)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "editImage",
               new SqlParameter("@imgID", img.id),
                new SqlParameter("@imgFile", img.file),
                new SqlParameter("@imgDesc", img.desc),
                new SqlParameter("@imgLink", img.link),
                new SqlParameter("@moduleID", img.modID),
                new SqlParameter("@imgsecID", img.imgsecID));
        }

        public override System.Data.IDataReader GetImageSections(int moduleId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "getImageSections",
                new SqlParameter("@moduleID", moduleId));
        }

        public override System.Data.IDataReader GetImageSection(int imgsecId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "getImageSection",
                new SqlParameter("@imgsecID", imgsecId));
        }
        public override int AddImageSection(cImageSection imgsec)
        {
            return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, NamePrefix + "addImageSection",
                 new SqlParameter("@imgsecDesc", imgsec.desc),
                 new SqlParameter("@moduleID", imgsec.modID)));
        }

        public override void DeleteImageSection(int imgsecId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "deleteImageSection",
                new SqlParameter("@imgsecID", imgsecId));
        }

        public override void UpdateImageSection(cImageSection imgsec)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "editImageSection",
               new SqlParameter("@imgsecID", imgsec.id),               
                new SqlParameter("@imgsecDesc", imgsec.desc));
        }

        public override void DeleteSectionImages(int imgsecId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, NamePrefix + "deleteSectionImages",
                new SqlParameter("@imgsecID", imgsecId));
        }

        #endregion

    }

}